<%@ page language="java"  pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="shiro" uri="http://shiro.apache.org/tags" %>
<script type="text/javascript" charset="utf-8">
	$(function() {
		$.extend($.fn.validatebox.defaults.rules, {
			minLength: { // 判断最小长度
				          validator: function (value, param) {
				              return value.length >= param[0];
				         },
				          message: '最少输入 {0} 个字符'
				     },
		     phoneOrMobile: {//验证手机或电话
				    	  validator: function (value) {
				    	     return /^(13|15|18)\d{9}$/i.test(value) || /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
				    	 },
				    	    message: '请填入手机或电话号码,如13688888888或020-8888888'
				    },
			chinese : {// 验证中文
	                     validator : function(value) {
				    	     return /^[\u0391-\uFFE5]+$/i.test(value);
				    	  },
				    	    message : '请输入中文'
				    },
			logname: {// 验证用户名
				    	 validator: function (value) {
				    	     return /^[a-zA-Z][a-zA-Z0-9_]{2,12}$/i.test(value);
				    	  },
				    	    message: '用户名不合法（字母开头，允许3-13字节，允许字母数字下划线）'
		           },

		});
		editRow=undefined;
		$('#admin_yhgl_datagrid').datagrid({
			url : ' ${pageContext.request.contextPath}/userAction!datagrid.action ',
			title : '',
			iconCls : 'icon-save',
			pagination : true,
			pageSize : 5,
			pageList : [ 5, 10, 15, 20 ],
			fit : true,
			fitColumns : true,
			nowarp : false,
			border : false,
			idField : 'id',
			checkOnSelect:true,
			selectOnCheck:true,
			/* frozenColumns:[[{
                field : 'name',
                title : '登录名',
                width : 100 
                }]], */
			columns : [ [ {
				field : 'id',
				title : '编号',
				width : 100,
				checkbox:true
			}, {
				field : 'logname',
				title : '登录名',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填',
						validType : 'logname'
						}
			           }
			},{
				field : 'name',
				title : '姓名',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填',
						validType : 'chinese'
						}
			           }
			}, {
				field : 'telephone',
				title : '联系方式',
				width : 100,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填',
						validType : 'phoneOrMobile'
						}
			           }
			}, {
				field : 'gender',
				title : '性别',
				width : 100,
				sortable :true,
				editor:{
					type:'combobox',
					options:{
						panelHeight:'auto',
						editable:false,
						valueField: 'label',
						textField: 'value',
						data: [{
							label: '男',
							value: '男'
						},{
							label: '女',
							value: '女'
						}],
						required:true,
						missingMessage:'该项必填'
						}
			           }
			}, {
				field : 'rolename',
				title : '角色',
				width : 100,
				editor:{
					type:'combobox',
					options:{
						required:true,
						missingMessage:'该项必填',
						editable:false,
						valueField:'id',
						textField:'rolename',
						url:' ${pageContext.request.contextPath}/roleAction!getRoleList.action',
						panelHeight:'auto'
						}
			           }
			}, {
				field : 'pwd',
				title : '密码',
				width : 100,
				formatter: function(value,row,index){
					return '********';
				} ,
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填',
						validType : 'minLength[6]'
						}
			           }
			}, {
				field : 'createdatetime',
				title : '创建时间',
				width : 100,
				sortable:true,
				editor:{
					type:'datebox',
					options:{
						//required:true,
						editable:false
						}
			           }
			}, {
				field : 'updatedatetime',
				title : '最后修改时间',
				width : 100,
				sortable:true,
				editor:{
					type:'datebox',
					options:{
						//required:true,
						editable:false
						}
			           }
			} ] ],
			toolbar:'#toolbar',
			onAfterEdit:function(rowIndex,rowData,changes){
				var inserted=$('#admin_yhgl_datagrid').datagrid('getChanges','inserted');
				if (inserted.length < 1) {
					editRow = undefined;
					datagrid.datagrid('unselectAll');
					return;
				}
				var url=' ';
				if(inserted.length>0){
					url='userAction!add.action';
				}
				$.ajax({
					url:url,
					data:rowData,
					dataType:'json',
					success:function(r){
						if(r&&r.success){
							$('#admin_yhgl_datagrid').datagrid('acceptChanges');
							$('#admin_yhgl_datagrid').datagrid('load');
						    $.messager.show({
						    	title:'成功',
						    	msg:r.msg
						    	});
						}else{
							$('#admin_yhgl_datagrid').datagrid('rejectChanges');
						    $.messager.alert('错误',r.msg,'error');
						}
						editRow=undefined;
						$('#admin_yhgl_datagrid').datagrid('unselectAll');
					}
				});
			},
			onRowContextMenu : function(e, rowIndex, rowData) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#menu').menu('show', {
					left : e.pageX,
					top : e.pageY
				});
			}	
		});
		$('#admin_yhgl_searchbox').searchbox({ //搜索框选项
		    searcher:function(value,name){
		    	searchFun();
		    },
		    menu:'#mm',
		    prompt:'可进行模糊查询'
		});
		 addFun=function(){ //添加用户
			 if(editRow!=undefined){
					$('#admin_yhgl_datagrid').datagrid('endEdit',editRow);
				}  
				if(editRow==undefined){
					$('#admin_yhgl_datagrid').datagrid('unselectAll');	
				    changeEditorAddRow();
				    $('#admin_yhgl_datagrid').datagrid('insertRow',{
					       index:0,
					       row:{}
					 });
				$('#admin_yhgl_datagrid').datagrid('beginEdit',0);
				$('#admin_yhgl_datagrid').datagrid('unselectAll');
				editRow=0;
			    }
		    };
		 delFun=function(){ //删除用户
			 var rows=$('#admin_yhgl_datagrid').datagrid('getChecked');
				if(rows.length>0){
					$.messager.confirm("请确认","您确定要删除当前所选择的记录吗?",function(b){
						if(b){
							var ids=[];
							for(var i=0;i<rows.length;i++){
								ids.push(rows[i].id);
							}
							//console.info(ids.join(','));
							$.ajax({
								url:'userAction!del.action',
								data:{
									ids:ids.join(',')
									},
								dataType:'json',
								success:function(r){
									if(r&&r.success){
										$('#admin_yhgl_datagrid').datagrid('load');
										$('#admin_yhgl_datagrid').datagrid('unselectAll');
									    $.messager.show({
									    	title:'提示',
									    	msg:r.msg
									    	});
									}else{
										$('#admin_yhgl_datagrid').datagrid('rejectChanges');
										$('#admin_yhgl_datagrid').datagrid('unselectAll');
									    $.messager.alert('错误',r.msg,'error');
									}
								}
							});
						}
					});
				}else{
					  $.messager.alert('警告','未选中任何记录,请选择要删除的记录!','warning');			
				} 
		 };
		 editFun=function(){ //编辑用户
			 edit();
		 };
		 saveFun=function(){ //保存
			 $('#admin_yhgl_datagrid').datagrid('endEdit',editRow);
		 };
		 undoEdit=function(){ //取消编辑
			 editRow=undefined;
			 $('#admin_yhgl_datagrid').datagrid('rejectChanges');
			 $('#admin_yhgl_datagrid').datagrid('unselectAll');
			 $('#admin_yhgl_datagrid').datagrid('uncheckAll');
		 };
		 unSeclect=function(){ //取消选中
			 $('#admin_yhgl_datagrid').datagrid('unselectAll');
			 $('#admin_yhgl_datagrid').datagrid('uncheckAll');
		 };
		 refresh=function(){ //刷新
			 $('#admin_yhgl_datagrid').datagrid('reload');
		 };
		 edit=function() { //编辑弹窗
			var rows = $('#admin_yhgl_datagrid').datagrid('getSelections');
			if (rows.length == 1) {
				var p = parent.dialog({
					title : '编辑用户',
					href : '${pageContext.request.contextPath}/userAction!userEdit.action',
					width : 280,
					height : 350,
					buttons : [ {
						text : '编辑',
						iconCls :'icon-edit',
						handler : function() {
							var f = p.find('form');
							f.form('submit', {
								url : '${pageContext.request.contextPath}/userAction!editor.action',
								success : function(d) {
									var json = $.parseJSON(d);
									if (json.success) {
										$('#admin_yhgl_datagrid').datagrid('reload');
										p.dialog('close');
										$('#admin_yhgl_datagrid').datagrid('unselectAll');
									}
									parent.messagerShow({
										msg : json.msg,
										title : '提示'
									});
								}
							});
						}
					} ],
					onLoad : function() {
						var f = p.find('form');
						f.form('load', {
							id : rows[0].id,
							logname : rows[0].logname,
							name : rows[0].name,
							gender : rows[0].gender,
							telephone : rows[0].telephone,
							roleid : rows[0].roleid,
							createdatetime :rows[0].createdatetime,
							updatedatetime:rows[0].updatedatetime
						});
					}
				});
			} else if (rows.length > 1) {
				 $.messager.alert('提示', '同一时间只能编辑一条记录！', 'error');
				 $('#admin_yhgl_datagrid').datagrid('unselectAll');
			} else {
				 $.messager.alert('提示', '请选择要编辑的记录！', 'error');
			}
		}
		/* $('#admin_yhgl_datagrid').datagrid('enableFilter'); */
		changeEditorAddRow=function(){
			$('#admin_yhgl_datagrid').datagrid('addEditor',{
				field:'pwd',
				editor:{
					type:'validatebox',
					options:{
						required:true,
						missingMessage:'该项必填',
						validType : 'minLength[6]'
					}
				}
			});
			$('#admin_yhgl_datagrid').datagrid('removeEditor',['createdatetime','updatedatetime','rolename']);
		};
	});
	
	function searchFun() {
		//console.info(serializeObject($('#admin_yhgl_searchForm')));
		$('#admin_yhgl_datagrid').datagrid('load',serializeObject($('#admin_yhgl_searchForm')));
	}
	function clearFun() {
		$('#admin_yhgl_datagrid').datagrid('load', {});
	}
</script>
<div id="admin_yhgl_layout" class="easyui-layout" data-options="border:false,fit:true">
<div region="north" data-options="border:false,title:'过滤信息'" style="height:75px;overflow:hidden;">
<form id="admin_yhgl_searchForm" >
<div style="float:right;margin:5px;"><input id="admin_yhgl_searchbox" style="width:300px;"></input></div><br/>
<div style="float:left;margin:5px;"><label>创建时间&nbsp;:&nbsp;从&nbsp;</label><input name="createdatetimeStart"  class="easyui-datebox" /><label>&nbsp;至&nbsp;</label><input name="createdatetimeEnd" class="easyui-datebox" />
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="searchFun();" >查询</a>
<a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="clearFun();">回显所有</a></div>
</form>
<div id="mm" style="width:120px">
    <div data-options="name:'name',iconCls:'icon-ok'">姓名</div>
    <div data-options="name:'logname',iconCls:'icon-ok'">登录名</div>
</div>
</div>
<div id="toolbar" style="display: none;">
		<table>
			<tr>
				<shiro:hasPermission name="sys:yhgl:add">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-add',plain:true" onclick="addFun();">添加</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:yhgl:del">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-remove',plain:true" onclick="delFun();">删除</a></td>
				</shiro:hasPermission>
				<shiro:hasPermission name="sys:yhgl:edit">
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-edit',plain:true" onclick="editFun();">修改</a></td>
				</shiro:hasPermission>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:true" onclick="saveFun();">保存</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="undoEdit();">取消编辑</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-undo',plain:true" onclick="unSelect();">取消选中</a></td>
				<td><a href="javascript:void(0);" class="easyui-linkbutton" data-options="iconCls:'icon-reload',plain:true" onclick="refresh();">刷新</a></td>
			</tr>
		</table>
	</div>
<div region="center" data-options="border:false">
<table id="admin_yhgl_datagrid"></table>
</div>
<shiro:hasPermission name="sys:yhgl:edit">
<div id="menu" class="easyui-menu" style="width: 120px; display: none;">
<div onclick="edit();" data-options="iconCls:'icon-edit'">编辑</div>
</div>
</shiro:hasPermission>
</div>


